/*
 * File FilesToSendModel.java 
 */
package cs.model;

import java.util.ArrayList;
import java.util.Observable;

import cs.entity.UserFile;

/**
 * This class represents a queue of files to send.<br>
 * It is updated by the GUI and observable by the network interface.<p>
 * Package  cs.model
 * Project ChatSystemG1 - INSA 4INFOA1 - 2013/2014
 * @author Julie DUMONS & Juste OUEDRAOGO
 */
public class FileToSendModel extends Observable {
	/**
	 * files to send manager
	 */
	private ArrayList<UserFile> filesToSend;
	
	
	/**
	 * constructor
	 */
	public FileToSendModel(){
		filesToSend = new ArrayList<UserFile>();
	}
	
	/**
	 * add a file to the sending queue
	 * @param file the file to be added
	 */
	public void addFileToSend(UserFile file){
		if (filesToSend != null) {
			filesToSend.add(file);
			setChanged();
			notifyObservers(file);	
			System.out.println(filesToSend);
		}
		
	}
	
	/**
	 * get the file to be sent (containing the file choosen by the localuser)
	 * @param file userfile may not contains the file to send
	 * @return the file to send
	 */
	public UserFile getFile(UserFile file){
		for(UserFile f : filesToSend){
			if(f.equals(file)) return f;
		}
		return null;
	}
	
	/**
	 * change the state of the file
	 * @param file which state is to be changed
	 * @param state the new state
	 */
	public void setFileState(UserFile file, UserFile.State state){
		if(filesToSend.contains(file)){
			file.setState(state);
			setChanged();
			notifyObservers(file);
		}else{
			System.err.println("file not found ");
		}
	}
	/**
	 * remote the file sent from the model
	 * @param file the file to remove
	 */
	public void removeFile(UserFile file){
		if(filesToSend.contains(file)){
			filesToSend.remove(file);
		}
		else{
			System.out.println("cannot remove file");
		}
	}
	
}
